"""
Case Type   : 小型化
Case Name   : gs_restore 获取帮助信息
Create At   : 2025/1/22
Owner       : wan005
Description :
    1.gs_restore 获取帮助信息：gs_restore -?
Expect      :
    1.查看到参数说明
History     :
"""
import os
import time
import unittest
from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.root_node = Node('PrimaryRoot')
        self.user_node = Node('PrimaryDbUser')
        self.user = None
        self.port = None

    def test_audit(self):
        text = '----prestep1:获取可用用户和端口;expect:获取成功----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(self.root_node)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        self.user_node.ssh_user = self.user
        self.user_node.ssh_password = macro.COMMON_PASSWD
        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.audit_path = os.path.join(self.data_path, 'pg_audit')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, self.root_node)

        text = '----prestep3:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_node)
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----prestep4:执行脚本安装数据库;expect:安装成功----'
        self.log.info(text)
        res = self.com_install.install_single(self.user_node, self.port)
        self.log.info(res)
        self.assertIn('start datanode success', res, f'执行失败: {text}')

        text = '----step1:执行查看帮助信息命令;except:显示详细的帮助信息----'
        self.log.info(text)
        dump_cmd = f'gs_restore -?'
        dump_msg = self.user_node.sh(dump_cmd).result()
        self.log.info(dump_msg)
        params = ['-d,', '--dbname=NAME', '-f,', '--file=FILENAME', '-F,',
                     '--format=c|d|t', '-l,', '--list', '-v,', '--verbose',
                     '-V,', '--version', '-?,', '--help', '-a,', '--data-only',
                     '-c,', '--clean', '-C,', '--create', '-e,',
                     '--exit-on-error', '-I,', '--index=NAME', '-j,',
                     '--jobs=NUM', '-L,', '--use-list=FILENAME', '-n,',
                     '--schema=NAME', '-O,', '--no-owner', '-P,',
                     '--function=NAME(args)', '-s,', '--schema-only', '-S,',
                     '--sysadmin=NAME', '-t,', '--table=NAME', '-T,',
                     '--trigger=NAME', '-x,', '--no-privileges/--no-acl',
                     '-1,', '--single-transaction', '--disable-triggers',
                     '--no-data-for-failed-tables', '--no-security-labels',
                     '--no-tablespaces', '--section=SECTION',
                     '--use-set-session-authorization', '--pipeline', '-h,',
                     '--host=HOSTNAME', '-p,', '--port=PORT', '-U,',
                     '--username=NAME', '-w,', '--no-password', '-W,',
                     '--password=PASSWORD', '--role=ROLENAME',
                     '--rolepassword=ROLEPASSWORD', '--with-decryption= type',
                     '--with-key=KEY', '--with-salt=RANDVALUES',
                     '--with-salt=RANDVALUES',
                     '--with-module-params=MODULE_TYPE=TYPE']
        result = all(set(i).issubset(dump_msg) for i in params)
        self.assertEqual(result, True, f'执行失败: {text}')

    def tearDown(self):
        text = '-----清理环境-----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(self.user_node)
        self.com_uninstall.clear_env(self.root_node)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
